package windows;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import langBundle.MsgControl;

import xml.XmlWebSet;

public class SetupPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SpringLayout springLayout_3;
	private SpringLayout springLayout_2;
	private SpringLayout springLayout_1;
	private SpringLayout springLayout;
	private final JPanel panel = new JPanel();
	private final JLabel webLabel = new JLabel();
	private final JTextField webNameField = new JTextField();
	private final JPanel ftpPanel = new JPanel();
	private final JPanel dbPanel = new JPanel();
	private final JLabel ftpNameLabel = new JLabel();
	private final JTextField ftpNameField = new JTextField();
	private final JLabel ftpUserLabel = new JLabel();
	private final JTextField ftpUserNameField = new JTextField();
	private final JLabel ftpPswLabel = new JLabel();
	private final JLabel dbLinkLabel = new JLabel();
	private final JTextField dbLinkField = new JTextField();
	private final JLabel dbNameLabel = new JLabel();
	private final JTextField dbNameField = new JTextField();
	private final JLabel dbUserLabel = new JLabel();
	private final JTextField dbUserField = new JTextField();
	private final JLabel dbPswLabel = new JLabel();
	private final JPasswordField dbPswField = new JPasswordField();
	private final JPasswordField ftpPswField = new JPasswordField();
	private final JButton nextButton = new JButton();
	private final JButton cancelButton = new JButton();
	private final JLabel defDirLabel = new JLabel();
	private final JTextField defHomeDirField = new JTextField();
	private XmlWebSet xmSet;
	private HashMap <String,String> useWebSettings  = new HashMap<String,String>();
	private MsgControl msgBundle;
	private String pageUpdate = null;
	
	/**
	 * Create the panel
	 */
	public SetupPanel(XmlWebSet xmSet,MsgControl msgBundle) {
		super();
		this.xmSet = xmSet;
		this.msgBundle = msgBundle;
		try {
			jbInit();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		//
	}
	
	public SetupPanel(XmlWebSet xmSet,MsgControl msgBundle, String pageUpdate) {
		super();
		this.xmSet = xmSet;
		this.msgBundle = msgBundle;
		this.pageUpdate = pageUpdate;
		try {
			jbInit();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		//
	}
	
	private void jbInit() throws Exception {
		springLayout = new SpringLayout();
		setLayout(springLayout);
		
		add(panel);
		springLayout.putConstraint(SpringLayout.SOUTH, panel, 112, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 22, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, panel, -8, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.WEST, panel, 20, SpringLayout.WEST, this);
		springLayout_1 = new SpringLayout();
		panel.setLayout(springLayout_1);
		TitledBorder webPageTB = new TitledBorder(null, "Web Page", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, new Color(0, 0, 102));
		msgBundle.setMsgByName(webPageTB,"webPageTB");
		panel.setBorder(webPageTB);
		
		
		panel.add(webLabel);
		springLayout_1.putConstraint(SpringLayout.NORTH, webLabel, 5, SpringLayout.NORTH, panel);
		springLayout_1.putConstraint(SpringLayout.WEST, webLabel, 5, SpringLayout.WEST, panel);
		webLabel.setText("Web Page Name (example: www.pagename.com)");
		msgBundle.setMsgByName(webLabel,"webLabel");
		
		if(pageUpdate != null)
			webNameField.setText(xmSet.getWebPageParameter(pageUpdate, "webAddress"));
		panel.add(webNameField);
		springLayout_1.putConstraint(SpringLayout.SOUTH, webNameField, 24, SpringLayout.SOUTH, webLabel);
		springLayout_1.putConstraint(SpringLayout.NORTH, webNameField, 5, SpringLayout.SOUTH, webLabel);
		springLayout_1.putConstraint(SpringLayout.EAST, webNameField, 673, SpringLayout.WEST, panel);
		springLayout_1.putConstraint(SpringLayout.WEST, webNameField, 5, SpringLayout.WEST, panel);
		
		add(ftpPanel);
		springLayout_2 = new SpringLayout();
		ftpPanel.setLayout(springLayout_2);
		springLayout.putConstraint(SpringLayout.EAST, ftpPanel, -8, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.WEST, ftpPanel, 20, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, ftpPanel, 349, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.NORTH, ftpPanel, 134, SpringLayout.NORTH, this);
		TitledBorder ftpSetupTB = new TitledBorder(null, "FTP Setup", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, new Color(0, 0, 102));
		msgBundle.setMsgByName(ftpSetupTB,"ftpSetupTB");
		ftpPanel.setBorder(ftpSetupTB);
		
		ftpPanel.add(ftpNameLabel);
		springLayout_2.putConstraint(SpringLayout.NORTH, ftpNameLabel, 5, SpringLayout.NORTH, ftpPanel);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpNameLabel, 5, SpringLayout.WEST, ftpPanel);
		ftpNameLabel.setText("FTP Server Name or IP");
		msgBundle.setMsgByName(ftpNameLabel,"ftpNameLabel");
		
		if(pageUpdate != null)
			ftpNameField.setText(xmSet.getWebPageParameter(pageUpdate, "ftpName"));
		ftpPanel.add(ftpNameField);
		springLayout_2.putConstraint(SpringLayout.SOUTH, ftpNameField, 24, SpringLayout.SOUTH, ftpNameLabel);
		springLayout_2.putConstraint(SpringLayout.NORTH, ftpNameField, 5, SpringLayout.SOUTH, ftpNameLabel);
		springLayout_2.putConstraint(SpringLayout.EAST, ftpNameField, 673, SpringLayout.WEST, ftpNameLabel);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpNameField, 0, SpringLayout.WEST, ftpNameLabel);
		
		ftpPanel.add(ftpUserLabel);
		springLayout_2.putConstraint(SpringLayout.EAST, ftpUserLabel, 335, SpringLayout.WEST, ftpPanel);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpUserLabel, 0, SpringLayout.WEST, ftpNameField);
		ftpUserLabel.setText("FTP User Name");
		msgBundle.setMsgByName(ftpUserLabel,"ftpUserLabel");
		
		if(pageUpdate != null)
			ftpUserNameField.setText(xmSet.getWebPageParameter(pageUpdate, "ftpUserName"));
		ftpPanel.add(ftpUserNameField);
		springLayout_2.putConstraint(SpringLayout.SOUTH, ftpUserLabel, -5, SpringLayout.NORTH, ftpUserNameField);
		springLayout_2.putConstraint(SpringLayout.EAST, ftpUserNameField, 335, SpringLayout.WEST, ftpUserLabel);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpUserNameField, 0, SpringLayout.WEST, ftpUserLabel);
		
		ftpPanel.add(ftpPswLabel);
		springLayout_2.putConstraint(SpringLayout.SOUTH, ftpUserNameField, -5, SpringLayout.NORTH, ftpPswLabel);
		springLayout_2.putConstraint(SpringLayout.NORTH, ftpUserNameField, -24, SpringLayout.NORTH, ftpPswLabel);
		springLayout_2.putConstraint(SpringLayout.EAST, ftpPswLabel, 81, SpringLayout.WEST, ftpUserNameField);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpPswLabel, 0, SpringLayout.WEST, ftpUserNameField);
		ftpPswLabel.setText("FTP password");
		msgBundle.setMsgByName(ftpPswLabel,"ftpPswLabel");
		
		if(pageUpdate != null)
			ftpPswField.setText(xmSet.getWebPageParameter(pageUpdate, "ftpPsw"));
		ftpPanel.add(ftpPswField);
		springLayout_2.putConstraint(SpringLayout.SOUTH, ftpPswLabel, -5, SpringLayout.NORTH, ftpPswField);
		springLayout_2.putConstraint(SpringLayout.EAST, ftpPswField, 335, SpringLayout.WEST, ftpPswLabel);
		springLayout_2.putConstraint(SpringLayout.WEST, ftpPswField, 0, SpringLayout.WEST, ftpPswLabel);
		springLayout_2.putConstraint(SpringLayout.SOUTH, ftpPswField, 180, SpringLayout.NORTH, ftpPanel);
		springLayout_2.putConstraint(SpringLayout.NORTH, ftpPswField, 161, SpringLayout.NORTH, ftpPanel);
		
		ftpPanel.add(defDirLabel);
		springLayout_2.putConstraint(SpringLayout.NORTH, defDirLabel, 5, SpringLayout.SOUTH, ftpNameField);
		springLayout_2.putConstraint(SpringLayout.WEST, defDirLabel, 0, SpringLayout.WEST, ftpNameField);
		defDirLabel.setText("Default Home Directory");
		msgBundle.setMsgByName(defDirLabel,"defDirLabel");
		
		if(pageUpdate != null)
			defHomeDirField.setText(xmSet.getWebPageParameter(pageUpdate, "defHomeDir"));
		ftpPanel.add(defHomeDirField);
		springLayout_2.putConstraint(SpringLayout.EAST, defHomeDirField, 0, SpringLayout.EAST, ftpUserNameField);
		springLayout_2.putConstraint(SpringLayout.WEST, defHomeDirField, 0, SpringLayout.WEST, defDirLabel);
		springLayout_2.putConstraint(SpringLayout.NORTH, defHomeDirField, 5, SpringLayout.SOUTH, defDirLabel);
		
		add(dbPanel);
		springLayout_3 = new SpringLayout();
		dbPanel.setLayout(springLayout_3);
		springLayout.putConstraint(SpringLayout.EAST, dbPanel, -8, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.WEST, dbPanel, 20, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, dbPanel, 626, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.NORTH, dbPanel, 371, SpringLayout.NORTH, this);
		TitledBorder dbSetupTB = new TitledBorder(null, "Database Setup", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, new Color(0, 0, 102));
		msgBundle.setMsgByName(dbSetupTB,"dbSetupTB");
		dbPanel.setBorder(dbSetupTB);
		
		dbPanel.add(dbLinkLabel);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbLinkLabel, 5, SpringLayout.NORTH, dbPanel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbLinkLabel, 5, SpringLayout.WEST, dbPanel);
		dbLinkLabel.setText("Database Link or IP");
		msgBundle.setMsgByName(dbLinkLabel,"dbLinkLabel");
		
		if(pageUpdate != null)
			dbLinkField.setText(xmSet.getWebPageParameter(pageUpdate, "dbLink"));
		dbPanel.add(dbLinkField);
		springLayout_3.putConstraint(SpringLayout.SOUTH, dbLinkField, 24, SpringLayout.SOUTH, dbLinkLabel);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbLinkField, 5, SpringLayout.SOUTH, dbLinkLabel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbLinkField, 663, SpringLayout.WEST, dbLinkLabel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbLinkField, 0, SpringLayout.WEST, dbLinkLabel);
		
		dbPanel.add(dbNameLabel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbNameLabel, 120, SpringLayout.WEST, dbPanel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbNameLabel, 0, SpringLayout.WEST, dbLinkField);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbNameLabel, 55, SpringLayout.NORTH, dbPanel);
		dbNameLabel.setText("Database Name");
		msgBundle.setMsgByName(dbNameLabel,"dbNameLabel");
		
		if(pageUpdate != null)
			dbNameField.setText(xmSet.getWebPageParameter(pageUpdate, "dbName"));
		dbPanel.add(dbNameField);
		springLayout_3.putConstraint(SpringLayout.SOUTH, dbNameField, 24, SpringLayout.SOUTH, dbNameLabel);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbNameField, 5, SpringLayout.SOUTH, dbNameLabel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbNameField, 320, SpringLayout.WEST, dbNameLabel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbNameField, 0, SpringLayout.WEST, dbNameLabel);
		
		dbPanel.add(dbUserLabel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbUserLabel, 0, SpringLayout.EAST, dbNameLabel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbUserLabel, 0, SpringLayout.WEST, dbNameField);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbUserLabel, 105, SpringLayout.NORTH, dbPanel);
		dbUserLabel.setText("Database User");
		msgBundle.setMsgByName(dbUserLabel,"dbUserLabel");
		
		if(pageUpdate != null)
			dbUserField.setText(xmSet.getWebPageParameter(pageUpdate, "dbUser"));
		dbPanel.add(dbUserField);
		springLayout_3.putConstraint(SpringLayout.SOUTH, dbUserField, 143, SpringLayout.NORTH, dbPanel);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbUserField, 124, SpringLayout.NORTH, dbPanel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbUserField, 320, SpringLayout.WEST, dbUserLabel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbUserField, 0, SpringLayout.WEST, dbUserLabel);
		
		dbPanel.add(dbPswLabel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbPswLabel, 130, SpringLayout.WEST, dbPanel);
		springLayout_3.putConstraint(SpringLayout.WEST, dbPswLabel, 0, SpringLayout.WEST, dbUserField);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbPswLabel, 155, SpringLayout.NORTH, dbPanel);
		dbPswLabel.setText("Database Password");
		msgBundle.setMsgByName(dbPswLabel,"dbPswLabel");
		
		if(pageUpdate != null)
			dbPswField.setText(xmSet.getWebPageParameter(pageUpdate, "dbPsw"));
		dbPanel.add(dbPswField);
		springLayout_3.putConstraint(SpringLayout.SOUTH, dbPswField, 193, SpringLayout.NORTH, dbPanel);
		springLayout_3.putConstraint(SpringLayout.NORTH, dbPswField, 174, SpringLayout.NORTH, dbPanel);
		springLayout_3.putConstraint(SpringLayout.EAST, dbPswField, 0, SpringLayout.EAST, dbUserField);
		springLayout_3.putConstraint(SpringLayout.WEST, dbPswField, 0, SpringLayout.WEST, dbPswLabel);
		
		add(nextButton);
		nextButton.addActionListener(new NextButtonActionListener());
		springLayout.putConstraint(SpringLayout.EAST, nextButton, -8, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, nextButton, -17, SpringLayout.SOUTH, this);
		nextButton.setText("Next");
		msgBundle.setMsgByName(nextButton,"nextButton");
		
		add(cancelButton);
		cancelButton.addActionListener(new CancelButtonActionListener());
		springLayout.putConstraint(SpringLayout.EAST, cancelButton, -5, SpringLayout.WEST, nextButton);
		springLayout.putConstraint(SpringLayout.SOUTH, cancelButton, -17, SpringLayout.SOUTH, this);
		cancelButton.setText("Cancel");
		msgBundle.setMsgByName(cancelButton,"cancelButton");
	}
	private class NextButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			nextButton_actionPerformed(arg0);
		}
	}
	private class CancelButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			cancelButton_actionPerformed(arg0);
		}
	}
	protected void nextButton_actionPerformed(ActionEvent arg0) {
		useWebSettings.put("webAddress", webNameField.getText());
		useWebSettings.put("ftpName", ftpNameField.getText());
		useWebSettings.put("defHomeDir", defHomeDirField.getText());
		useWebSettings.put("ftpUserName", ftpUserNameField.getText());
		useWebSettings.put("ftpPsw", new String(ftpPswField.getPassword()));
		useWebSettings.put("ftpUserName", ftpUserNameField.getText());
		useWebSettings.put("dbLink", dbLinkField.getText());
		useWebSettings.put("dbName", dbNameField.getText());
		useWebSettings.put("dbUser", dbUserField.getText());
		useWebSettings.put("dbPsw", new String(dbPswField.getPassword()));
		if(xmSet.webExistAlready(webNameField.getText()) && pageUpdate == null){
			JOptionPane.showConfirmDialog(null, "Page already exist", "Page exist", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
		}else if(pageUpdate == null && !xmSet.webExistAlready(webNameField.getText())){
			xmSet.updateXmlSetingsParameters(useWebSettings);
			JTabbedPane tabsPane = (JTabbedPane)this.getParent();
			tabsPane.remove(tabsPane.getSelectedComponent());
		}else{
			if(!pageUpdate.equals(webNameField.getText()) && xmSet.webExistAlready(webNameField.getText())){
				JOptionPane.showConfirmDialog(null, "Page already exist", "Page exist", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
			}else{
				if(pageUpdate.equals(webNameField.getText()))
				  xmSet.updateXmlSetingsParameters(useWebSettings);
				else
				  xmSet.updateXmlSetingsParameters(useWebSettings,pageUpdate);	
				JTabbedPane tabsPane = (JTabbedPane)this.getParent();
				tabsPane.remove(tabsPane.getSelectedComponent());
			}
		}
	}
	protected void cancelButton_actionPerformed(ActionEvent arg0) {
		JTabbedPane tabsPane = (JTabbedPane)this.getParent();
		tabsPane.remove(tabsPane.getSelectedComponent());
	}

}
